package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.file.MinIoTemplate;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import com.heima.wemedia.thread.UserThreadLocal;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;


@Slf4j
@Service
@Transactional
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {
    @Autowired
    private WmMaterialMapper wmMaterialMapper;

    @Autowired
    private MinIoTemplate minIoTemplate;

    //图片上传
    @Override
    public ResponseResult upLoad(MultipartFile file) throws IOException {
        //1.转为输入流
        InputStream inputStream = file.getInputStream();

        //2.图片上传,获取地址
        String contentType = file.getContentType();//image/png
        if (StringUtils.isEmpty(contentType)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_IMAGE_FORMAT_ERROR);//图片格式有误
        }
        String suffixName = file.getContentType().split("/")[1];//png
        String filename = System.currentTimeMillis() + "." + suffixName;
        String url = minIoTemplate.uploadFile("", filename, inputStream, "images");

        //3.创建素材,将图片地址写入到数据库
        WmMaterial wmMaterial = new WmMaterial();
        //从UserThreadLocal中取出userId
        wmMaterial.setUserId(UserThreadLocal.get().getId());
        wmMaterial.setCreatedTime(new Date());
        wmMaterial.setIsCollection((short) 0);//未收藏
        wmMaterial.setType((short) 0);//图片
        wmMaterial.setUrl(url);

        int insert = wmMaterialMapper.insert(wmMaterial);

        if (insert < 1) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_IMAGE_FORMAT_ERROR);//图片格式有误
        }
        //4.返回素材
        return ResponseResult.okResult(wmMaterial);
    }

    @Override
    public ResponseResult findList(WmMaterialDto dto) {
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        Short isCollection = dto.getIsCollection();
        Integer currentPage = dto.getPage();
        Integer size = dto.getSize();
        // 构造查询条件
        LambdaQueryWrapper<WmMaterial> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(isCollection == 1, WmMaterial::getIsCollection, isCollection);//是否收藏
        queryWrapper.eq(WmMaterial::getUserId, UserThreadLocal.get().getId());//查询当前用户
        queryWrapper.orderByDesc(WmMaterial::getCreatedTime);//按时间排序(倒序)
        // 构建分页条件
        Page<WmMaterial> page = new Page<>(currentPage, size);
        page = wmMaterialMapper.selectPage(page, queryWrapper);
        // 构建返回结果
        Long total = page.getTotal();
        PageResponseResult pageResponseResult = new PageResponseResult(currentPage, size, total.intValue());
        pageResponseResult.setData(page.getRecords());

        return pageResponseResult;
    }
}
